//
//  SearchPhoneCodeViewController.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/22/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "SearchPhoneCodeViewController.h"
#import "GlobalObject.h"

@interface SearchPhoneCodeViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SearchPhoneCodeViewController
{
    NSArray *allKey;
    NSArray *searchResult;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    allKey = [[GlobalObject phoneCode] allKeys];
    searchResult = [[NSArray alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma Tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResult count];
    } else {
        return [allKey count];
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
    }
    NSString *str = @"";
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        str = [searchResult objectAtIndex:indexPath.row];
    } else {
        str = [allKey objectAtIndex:indexPath.row];
    }
    
    cell.textLabel.text = str;
    cell.detailTextLabel.text = [[GlobalObject phoneCode] objectForKey:str];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *keyStr = @"";
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        keyStr = [searchResult objectAtIndex:indexPath.row];
    } else {
        keyStr = [allKey objectAtIndex:indexPath.row];
    }
    
    [GlobalObject setCountryName:keyStr];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    searchResult = [allKey filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];
    
    return YES;
}

- (IBAction)OnCancelClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
